﻿<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>配送范围</title>
    <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
    <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.1&key=897c37791dedfb7693f32913be501911&plugin=AMap.PolyEditor,AMap.MouseTool"></script>
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div id="container"></div>
<script>
	var arr = [];
    if(parent.$("#lng").val() && parent.$("#lat").val()){
		var marker,map = new AMap.Map("container", {
			zoom: 16,
			resizeEnable: true,
			center: [parent.$("#lng").val(), parent.$("#lat").val()]
		});
		marker = new AMap.Marker({
			icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
			position: [parent.$("#lng").val(), parent.$("#lat").val()]
		});
		marker.setMap(map);
		if(parent.$("#range").val()){
			arr = parent.$("#range").val();
			var arr = JSON.parse(arr);
			editor_init();
		}else{
			//在地图中添加MouseTool插件
			var mouseTool = new AMap.MouseTool(map);
			mouseTool.polygon();
			AMap.event.addListener(mouseTool,'draw',function(e){
				var path = e.obj.getPath();
				for(var i=0;i<path.length;i++){
					arr.push([path[i].lng,path[i].lat]);
				}
				editor_init();
				mouseTool.close(true);
			});
		}
	}else{
		alert("请先选择商家坐标");
		parent.layer.closeAll();
	}
	
	function editor_init(){
		var editor={};
		parent.$("#range").val(JSON.stringify(arr));
		editor._polygon=(function(){
			return new AMap.Polygon({
				map: map,
				path: arr,
				strokeColor: "#0000ff",
				strokeOpacity: 1,
				strokeWeight: 3,
				fillColor: "#f5deb3",
				fillOpacity: 0.35
			});
		})();
		map.setFitView();
		editor._polygonEditor= new AMap.PolyEditor(map, editor._polygon);
		editor._polygonEditor.open();
		AMap.event.addListener(editor._polygonEditor,'adjust',polygonEnd);
	}
	
	function polygonEnd(res){
        var arr = [];
        var path = res.target.getPath();
		for(var i=0;i<path.length;i++){
			arr.push([path[i].lng,path[i].lat]);
		}
		var arr = JSON.stringify(arr);
		parent.$("#range").val(arr);
    }
</script>
</body>
</html>